package mdb

import (
	"gopkg.in/mgo.v2/bson"
	"reflect"
	"strings"
)

func UpdateM(t any) bson.M {
	setParam := bson.M{}
	typeObject := reflect.TypeOf(t)
	valueObject := reflect.ValueOf(t)
	typeRef := typeObject.Elem()
	valueRef := valueObject.Elem()
	for i := 0; i < typeRef.NumField(); i++ {
		name := typeRef.Field(i).Name
		bsonTag := typeRef.Field(i).Tag.Get("bson")
		transientTag := typeRef.Field(i).Tag.Get("transient")
		if bsonTag != "" {
			name = strings.Split(bsonTag, ",")[0]
		}
		if name == "_id" || name == "create_time" {
			continue
		}
		if transientTag != "" {
			continue
		}
		setParam[strings.ToLower(name)] = valueRef.Field(i).Interface()
	}
	updateParam := bson.M{"$set": setParam}
	return updateParam
}
